package com.iflytek.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.entity.Classroom;
import com.iflytek.entity.ResultData;
import com.iflytek.query.ClassRoomQuery;
import com.iflytek.service.ClassroomService;
import com.iflytek.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("back/classroom")
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;

    @GetMapping("/list")
    public String list(){
        return "/classroom/list";
    }


    @ResponseBody
    @GetMapping("listData")
    public ResultData listData(ClassRoomQuery query,Integer curPage,Integer pageSize){
        //设置分页参数
        PageHelper.startPage(curPage,pageSize);
        //查询数据列表
        List<Classroom> classrooms = classroomService.getClassRoomList(query);
        //构建分页数据对象
        PageInfo<Classroom> pageInfo= new PageInfo<>(classrooms);
        //返回分页数据
        return Result.success(pageInfo.getTotal(),pageInfo.getList());

    }

    @GetMapping("add")
    public String addUI(){
        return "classroom/add";
    }

    @ResponseBody
    @PostMapping("add")
    public Map<String,Object> add(Classroom classroom){
//        Map<String,Object> retMap = new HashMap<>();
        if (classroom == null){
            return Result.failure("添加失败");
        }
        Classroom cr = classroomService.getClassRoomByName(classroom.getName());
        if (cr != null){
            return Result.failure("添加失败，教室名称已经存在！");
        }
        cr = classroomService.getClassRoomByNo(classroom.getClassNo());
        if (cr != null){
            return Result.failure("添加失败，教室编号已经存在！");
        }

        int num = classroomService.insertAll(classroom);
        if(num == 1){
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    @GetMapping("edit")
    public String editUI(Integer classroomId, Model model){
        Classroom classroom = classroomService.getClassRoomById(classroomId);
        model.addAttribute("classroom",classroom);
        return "classroom/edit";
    }

    @ResponseBody
    @PostMapping("edit")
    public Map<String,Object> edit(Classroom classroom){
        if (classroom == null || classroom.getId() == null){
            return Result.failure("编辑失败");
        }
        //查询教室信息
        Classroom oldClassRoom = classroomService.getClassRoomById(classroom.getId());
        if (oldClassRoom == null){
            return Result.failure("编辑失败");
        }
        //判断修改的教室名称或编号是否冲突
        if (!oldClassRoom.getName().equals(classroom.getName())){
            Classroom cr = classroomService.getClassRoomByName(classroom.getName());
            if (cr != null){
                return Result.failure("编辑失败，教室名称已存在！");
            }
        }

        if (!oldClassRoom.getClassNo().equals(classroom.getClassNo())){
            Classroom cr = classroomService.getClassRoomByNo(classroom.getClassNo());
            if (cr != null){
                return Result.failure("编辑失败，教室编号已存在！");
            }
        }

        int num = classroomService.update(classroom);
        if(num == 1){
            return Result.success("编辑成功");
        }
        return Result.failure("编辑失败");
    }

    @ResponseBody
    @PostMapping("delete")
    public Map<String,Object> delete(Integer classRoomId){
        if (classRoomId == null){
            return Result.failure("删除失败");
        }
        int num = classroomService.deleteById(classRoomId);
        if(num == 1){
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }


    @ResponseBody
    @PostMapping("batchDelete")
    public Map<String,Object> batchDelete(String classroomIds){
        if (StringUtils.isEmpty(classroomIds)){
            return Result.failure("删除失败");
        }
        int num = classroomService.deleteByIds(classroomIds.split(","));
        if(num == 1){
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }



}
